<?php  
	include("../includes/connect_db.php");
	include("../includes/core.php");

	$date = (isset($_POST["date"])) ? $_POST["date"] : "" ;
	$act = (isset($_POST["act"])) ? $_POST["act"] : "" ;

	switch ($act) {
		case 'dayviewroom':
			$grand_total = 0;

			$sql_list_date = "SELECT DATE_FORMAT(a.checkout, '%Y-%c-%d') AS 'date_checkout', 
			DATE_FORMAT(a.checkout, '%M %d, %Y') AS 'date_tampil'
			FROM front_office a 
			WHERE a.is_paid = 1 AND CONCAT(DATE_FORMAT(a.checkout, '%Y-%m-%d')) = '".$date."' 
			GROUP BY DATE_FORMAT(a.checkout, '%Y-%c-%d') ASC";
			$query_list_date = mysql_query($sql_list_date);
			$count_list_date = mysql_num_rows($query_list_date);
			if ($count_list_date > 0) {
				$query_list_date = mysql_query($sql_list_date);
				while ($baris_list_date = mysql_fetch_array($query_list_date)) {
					$sql_report = "SELECT * FROM front_office a 
					WHERE a.is_paid = 1 AND 
					DATE_FORMAT(a.checkout, '%Y-%c-%d') = '".$baris_list_date['date_checkout']."' 
					ORDER BY a.checkout ASC";
					$query_report = mysql_query($sql_report);
					$count_report = mysql_num_rows($query_report);
					if ($count_report > 0) {
						?>
						<?php
						$query_report = mysql_query($sql_report);
						$i = 1;
						while ($baris_report = mysql_fetch_array($query_report)) {
							$sql_service = "SELECT * FROM service_charge a WHERE a.idfront_office = '".$baris_report['id']."'";
							$query_service = mysql_query($sql_service);
							$count_service = mysql_num_rows($query_service);

							$sql_agent = "SELECT * FROM agent WHERE id = '$baris_report[idagent]'";
							$query_agent = mysql_query($sql_agent);
							$result_agent = mysql_fetch_array($query_agent);

							$data_room = getRoom($baris_report[idroom]);

							$diff = abs(strtotime($baris_report['checkout']) - strtotime($baris_report['checkin']));
							$days = floor($diff / (60*60*24));
							$price_kamar = $baris_report['real_paid'] * $days * $baris_report['banyak_orang'];

							$sub_grand_total = $price_kamar;

							?>
							<tr>
								<td class="al_center" valign="top" rowspan="<?php echo ($count_service+2) ?>"><?php echo $i ?></td>
								<td class="al_left" valign="middle" rowspan="<?php echo ($count_service+2) ?>"><?php echo $baris_report['nama'] ?></td>
								<td class="al_left" valign="middle" rowspan="<?php echo ($count_service+2) ?>"><?php echo $result_agent['nama'] ?></td>
								<td class="al_left" valign="middle"><?php echo $data_room['nama'] ?></td>
								<td class="al_right currency" valign="middle"><?php echo $baris_report['real_paid'] ?></td>
								<td class="al_center" valign="middle"><?php echo $baris_report['banyak_orang'] ?></td>
								<td class="al_center" valign="middle"><?php echo $days." days" ?></td>
								<td class="al_right currency" valign="middle"><?php echo $price_kamar ?></td>
							</tr>
							<?php
								$query_service = mysql_query($sql_service);
								while ($baris_service = mysql_fetch_array($query_service)) {
									$price_service = $baris_service['harga']*$baris_service['jumlah'];
									?>
									<tr>
										<td class="al_left" valign="middle"><?php echo $baris_service['nama'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $baris_service['harga'] ?></td>
										<td class="al_center" valign="middle">-</td>
										<td class="al_center" valign="middle"><?php echo $baris_service['jumlah'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $price_service ?></td>
									</tr>
									<?php
									$sub_grand_total = $sub_grand_total+$price_service;
								}
							?>
							<tr class="sub-grand-total">
								<td class="al_right" colspan="4">Amount</td>
								<td class="al_right currency"><?php echo $sub_grand_total ?></td>
							</tr>
							<?php
							$grand_total = $grand_total+$sub_grand_total;
							$i++;
						}
					}
				}

				?>
				<tr class="grand-total">
					<td class="al_right" colspan="7">Total Amount</td>
					<td class="al_right currency"><?php echo $grand_total ?></td>
				</tr>
				<?php
			}
			break;
			
		case 'monthviewroom':
			$grand_total = 0;

			$sql_list_date = "SELECT DATE_FORMAT(a.checkout, '%Y-%c-%d') AS 'date_checkout', 
			DATE_FORMAT(a.checkout, '%M %d, %Y') AS 'date_tampil'
			FROM front_office a 
			WHERE a.is_paid = 1 AND CONCAT(YEAR(a.checkout),'-',MONTH(a.checkout)) = '".$date."' 
			GROUP BY DATE_FORMAT(a.checkout, '%Y-%c-%d') ASC";
			$query_list_date = mysql_query($sql_list_date);
			$count_list_date = mysql_num_rows($query_list_date);
			if ($count_list_date > 0) {
				$query_list_date = mysql_query($sql_list_date);
				$i = 1;
				while ($baris_list_date = mysql_fetch_array($query_list_date)) {
					$sql_report = "SELECT * FROM front_office a 
					WHERE a.is_paid = 1 AND 
					DATE_FORMAT(a.checkout, '%Y-%c-%d') = '".$baris_list_date['date_checkout']."' 
					ORDER BY a.checkout ASC";
					$query_report = mysql_query($sql_report);
					$count_report = mysql_num_rows($query_report);
					if ($count_report > 0) {
						?>
						<tr class="list-date">
							<td colspan="8"><?php echo $baris_list_date['date_tampil'] ?></td>
						</tr>
						<?php
						$query_report = mysql_query($sql_report);
						while ($baris_report = mysql_fetch_array($query_report)) {
							$sql_service = "SELECT * FROM service_charge a WHERE a.idfront_office = '".$baris_report['id']."'";
							$query_service = mysql_query($sql_service);
							$count_service = mysql_num_rows($query_service);

							$sql_agent = "SELECT * FROM agent WHERE id = '$baris_report[idagent]'";
							$query_agent = mysql_query($sql_agent);
							$result_agent = mysql_fetch_array($query_agent);

							$data_room = getRoom($baris_report[idroom]);

							$diff = abs(strtotime($baris_report['checkout']) - strtotime($baris_report['checkin']));
							$days = floor($diff / (60*60*24));
							$price_kamar = $baris_report['real_paid'] * $days * $baris_report['banyak_orang'];

							$sub_grand_total = $price_kamar;

							?>
							<tr>
								<td class="al_center" valign="top" rowspan="<?php echo ($count_service+2) ?>"><?php echo $i ?></td>
								<td class="al_left" valign="top" rowspan="<?php echo ($count_service+2) ?>"><?php echo $baris_report['nama'] ?></td>
								<td class="al_left" valign="top" rowspan="<?php echo ($count_service+2) ?>"><?php echo $result_agent['nama'] ?></td>
								<td class="al_left" valign="middle"><?php echo $data_room['nama'] ?></td>
								<td class="al_right currency" valign="middle"><?php echo $baris_report['real_paid'] ?></td>
								<td class="al_center" valign="middle"><?php echo $baris_report['banyak_orang'] ?></td>
								<td class="al_center" valign="middle"><?php echo $days." days" ?></td>
								<td class="al_right currency" valign="middle"><?php echo $price_kamar ?></td>
							</tr>
							<?php
								$query_service = mysql_query($sql_service);
								while ($baris_service = mysql_fetch_array($query_service)) {
									$price_service = $baris_service['harga']*$baris_service['jumlah'];
									?>
									<tr>
										<td class="al_left" valign="middle"><?php echo $baris_service['nama'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $baris_service['harga'] ?></td>
										<td class="al_center" valign="middle">-</td>
										<td class="al_center" valign="middle"><?php echo $baris_service['jumlah'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $price_service ?></td>
									</tr>
									<?php
									$sub_grand_total = $sub_grand_total+$price_service;
								}
							?>
							<tr class="sub-grand-total">
								<td class="al_right" colspan="4">Amount</td>
								<td class="al_right currency"><?php echo $sub_grand_total ?></td>
							</tr>
							<?php
							$grand_total = $grand_total+$sub_grand_total;
							$i++;
						}
					}
				}

				?>
				<tr class="grand-total">
					<td class="al_right" colspan="7">Total Amount</td>
					<td class="al_right currency"><?php echo $grand_total ?></td>
				</tr>
				<?php
			}else{
				?>
				<tr class="alert alert-error">
					<td colspan="8">No result has shown.</td>
				</tr>
				<?php
			}
			break;

		case 'yearviewroom':
			$grand_total = 0;
			$sql_list_date = "SELECT DATE_FORMAT(a.checkout, '%Y-%c-%d') AS 'date_checkout', 
			DATE_FORMAT(a.checkout, '%M %d, %Y') AS 'date_tampil'
			FROM front_office a 
			WHERE a.is_paid = 1 AND CONCAT(YEAR(a.checkout)) = '".$date."' 
			GROUP BY DATE_FORMAT(a.checkout, '%Y-%c-%d') ASC";
			$query_list_date = mysql_query($sql_list_date);
			$count_list_date = mysql_num_rows($query_list_date);
			if ($count_list_date > 0) {
				$query_list_date = mysql_query($sql_list_date);
				$i = 1;
				while ($baris_list_date = mysql_fetch_array($query_list_date)) {
					$sql_report = "SELECT * FROM front_office a 
					WHERE a.is_paid = 1 AND 
					DATE_FORMAT(a.checkout, '%Y-%c-%d') = '".$baris_list_date['date_checkout']."' 
					ORDER BY a.checkout ASC";
					$query_report = mysql_query($sql_report);
					$count_report = mysql_num_rows($query_report);
					if ($count_report > 0) {
						?>
						<tr class="list-date">
							<td colspan="8"><?php echo $baris_list_date['date_tampil'] ?></td>
						</tr>
						<?php
						$query_report = mysql_query($sql_report);
						while ($baris_report = mysql_fetch_array($query_report)) {
							$sql_service = "SELECT * FROM service_charge a WHERE a.idfront_office = '".$baris_report['id']."'";
							$query_service = mysql_query($sql_service);
							$count_service = mysql_num_rows($query_service);

							$sql_agent = "SELECT * FROM agent WHERE id = '$baris_report[idagent]'";
							$query_agent = mysql_query($sql_agent);
							$result_agent = mysql_fetch_array($query_agent);

							$data_room = getRoom($baris_report[idroom]);

							$diff = abs(strtotime($baris_report['checkout']) - strtotime($baris_report['checkin']));
							$days = floor($diff / (60*60*24));
							$price_kamar = $baris_report['real_paid'] * $days * $baris_report['banyak_orang'];

							$sub_grand_total = $price_kamar;

							?>
							<tr>
								<td class="al_center" valign="top" rowspan="<?php echo ($count_service+2) ?>"><?php echo $i ?></td>
								<td class="al_left" valign="top" rowspan="<?php echo ($count_service+2) ?>"><?php echo $baris_report['nama'] ?></td>
								<td class="al_left" valign="top" rowspan="<?php echo ($count_service+2) ?>"><?php echo $result_agent['nama'] ?></td>
								<td class="al_left" valign="middle"><?php echo $data_room['nama'] ?></td>
								<td class="al_right currency" valign="middle"><?php echo $baris_report['real_paid'] ?></td>
								<td class="al_center" valign="middle"><?php echo $baris_report['banyak_orang'] ?></td>
								<td class="al_center" valign="middle"><?php echo $days." days" ?></td>
								<td class="al_right currency" valign="middle"><?php echo $price_kamar ?></td>
							</tr>
							<?php
								$query_service = mysql_query($sql_service);
								while ($baris_service = mysql_fetch_array($query_service)) {
									$price_service = $baris_service['harga']*$baris_service['jumlah'];
									?>
									<tr>
										<td class="al_left" valign="middle"><?php echo $baris_service['nama'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $baris_service['harga'] ?></td>
										<td class="al_center" valign="middle">-</td>
										<td class="al_center" valign="middle"><?php echo $baris_service['jumlah'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $price_service ?></td>
									</tr>
									<?php
									$sub_grand_total = $sub_grand_total+$price_service;
								}
							?>
							<tr class="sub-grand-total">
								<td class="al_right" colspan="4">Amount</td>
								<td class="al_right currency"><?php echo $sub_grand_total ?></td>
							</tr>
							<?php
							$grand_total = $grand_total+$sub_grand_total;
							$i++;
						}
					}
				}

				?>
				<tr class="grand-total">
					<td class="al_right" colspan="7">Total Amount</td>
					<td class="al_right currency"><?php echo $grand_total ?></td>
				</tr>
				<?php
			}else{
				?>
				<tr class="alert alert-error">
					<td colspan="8">No result has shown.</td>
				</tr>
				<?php
			}

			break;

		case 'yearviewrestaurant':
			$grand_total = 0;
			$sql_year = "SELECT id AS id_resto_real, tanggal, MONTH(tanggal) AS month_select, 
			DATE_FORMAT(tanggal, '%M, %Y') AS resto_date FROM resto 
			WHERE YEAR(tanggal) = '".$date."' 
			AND id IN (SELECT idresto FROM nota WHERE is_paid = 1 AND is_service_charge = 0) 
			AND id IN (SELECT idresto FROM resto_menu)
			GROUP BY MONTH(tanggal) ASC";
			$query_year = mysql_query($sql_year);
			$count_year = mysql_num_rows($query_year);
			if ($count_year > 0) {
				$query_year = mysql_query($sql_year);
				while ($baris_year = mysql_fetch_array($query_year)) {
					$sql_month = "SELECT id AS id_resto_list, tanggal, MONTH(tanggal), 
					DATE_FORMAT(tanggal, '%M, %Y') AS resto_date FROM resto 
					WHERE YEAR(tanggal) = '".$date."' AND MONTH(tanggal) = '".$baris_year['month_select']."'
					AND id IN (SELECT idresto FROM nota WHERE is_paid = 1 AND is_service_charge = 0) 
					AND id IN (SELECT idresto FROM resto_menu) ORDER BY MONTH(tanggal) ASC";
					$query_month = mysql_query($sql_month);
					$count_month = mysql_num_rows($query_month);
					if ($count_month > 0) {
						?>
						<tr class="list-date">
							<td colspan="5"><?php echo $baris_year['resto_date'] ?></td>
						</tr>
						<?php
						$sql_list_item = "SELECT * FROM resto_menu a
						WHERE a.idresto IN (SELECT id FROM resto WHERE YEAR(tanggal) = '".$date."' 
						AND MONTH(tanggal) = '".$baris_year['month_select']."') GROUP BY a.idmenu ASC";
						$query_list_item = mysql_query($sql_list_item);
						$count_list_item = mysql_num_rows($query_list_item);
						if ($count_list_item > 0) {
							$urut_menu = 1;
							$query_cek_item = mysql_query($sql_list_item);
							while ($baris_cek_item = mysql_fetch_array($query_list_item)) {
								$sql_cek_per_item = "SELECT b.id AS id_menu, b.nama AS nama_menu, 
								a.harga_ori AS harga_menu, a.harga AS harga_jual FROM resto_menu a
								LEFT JOIN menu b ON b.id = a.idmenu
								WHERE a.idmenu = ".$baris_cek_item['idmenu']." AND a.idresto IN 
								(SELECT id FROM resto WHERE YEAR(tanggal) = '".$date."' AND MONTH(tanggal) = '".$baris_year['month_select']."')";
								$query_cek_per_item = mysql_query($sql_cek_per_item);
								$hasil_cek_item = 0;
								while ($baris_cek_per_item = mysql_fetch_array($query_cek_per_item)) {
									if ($baris_cek_per_item[harga_menu] != $baris_cek_per_item['harga_jual']) {
										$hasil_cek_item++;
									}
								}
								if ($hasil_cek_item < 1) {
									$sql_detail_item = "SELECT b.id AS id_menu, b.nama AS nama_menu, a.harga_ori AS harga_menu, 
									SUM(a.jumlah) AS sum_item, (SUM(a.jumlah)*a.harga_ori) AS amount_item FROM resto_menu a
									LEFT JOIN menu b ON b.id = a.idmenu
									WHERE a.idmenu = ".$baris_cek_item['idmenu']." AND a.idresto IN 
									(SELECT id FROM resto WHERE YEAR(tanggal) = '".$date."' AND MONTH(tanggal) = '".$baris_year['month_select']."')";
									$query_detail_item = mysql_query($sql_detail_item);
									$baris_detail_item = mysql_fetch_array($query_detail_item);
									?>
									<tr class="list-item-main">
										<td class="al_center" valign="middle"><?php echo $urut_menu ?></td>
										<td class="al_left" valign="middle"><?php echo $baris_detail_item['nama_menu'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $baris_detail_item['harga_menu'] ?></td>
										<td class="al_center" valign="middle"><?php echo $baris_detail_item['sum_item'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $baris_detail_item['amount_item'] ?></td>
									</tr>
									<?php
									$grand_total = $grand_total+$baris_detail_item['amount_item'];
								}else{
									$sql_harga_miring = "SELECT b.id AS id_menu, b.nama AS nama_menu, 
									a.harga_ori AS harga_menu, a.harga AS harga_jual FROM resto_menu a
									LEFT JOIN menu b ON b.id = a.idmenu
									WHERE a.idmenu = ".$baris_cek_item['idmenu']." AND a.idresto IN 
									(SELECT id FROM resto WHERE YEAR(tanggal) = '".$date."' AND MONTH(tanggal) = '".$baris_year['month_select']."')
									GROUP BY a.harga DESC";
									$query_nama_miring = mysql_query($sql_harga_miring);
									$baris_nama_miring = mysql_fetch_array($query_nama_miring);
									?>
									<tr class="list-item-main">
										<td colspan="5" style="padding:0px;">
											<table width="100%" class="main-item-placer">
												<tr class="baris-main">
													<td class="al_center" valign="middle" width="10%"><?php echo $urut_menu ?></td>
													<td class="al_left" valign="middle" width="50%"><?php echo $baris_nama_miring['nama_menu'] ?></td>
													<td class="al_left" valign="middle" width="7%"><b><i>(original price)</i></b></td>
													<td class="al_left currency" valign="middle" width="13%" style="border-left:none"><?php echo $baris_nama_miring['harga_menu'] ?></td>
													<td class="al_right" valign="middle" width="20%" style="border-left:none"></td>
												</tr>

												<tr class="list-item-detail">
													<td colspan="5" style="padding:0px;">
														<table width="100%" class="detail-item-placer">
														<?php
														$query_harga_miring = mysql_query($sql_harga_miring);
														$urut_miring = 1;
														while ($baris_harga_miring = mysql_fetch_array($query_harga_miring)) {
															$sql_detail_miring = "SELECT SUM(a.jumlah) AS jumlah_item, a.harga, SUM(a.jumlah)*a.harga AS amount_miring, b.nama AS nama_menu 
															FROM resto_menu a LEFT JOIN menu b ON b.id = a.idmenu
															WHERE a.idmenu = ".$baris_cek_item['idmenu']." AND a.harga = '".$baris_harga_miring['harga_jual']."' AND a.idresto IN 
															(SELECT id FROM resto WHERE YEAR(tanggal) = '".$date."' AND MONTH(tanggal) = '".$baris_year['month_select']."')";
															$query_detail_miring = mysql_query($sql_detail_miring);
															$baris_detail_miring = mysql_fetch_array($query_detail_miring);
															?>
																<tr>
																	<td class="al_center" valign="middle" width="10%"></td>
																	<td class="al_left" valign="middle" width="50%"><i class="fa fa-angle-double-right"></i> <?php echo $baris_detail_miring['nama_menu']." (".$urut_miring.")" ?></td>
																	<td class="al_right currency" valign="middle" width="20%"><?php echo $baris_detail_miring['harga'] ?></td>
																	<td class="al_center item-sum" valign="middle" width="10%"><?php echo $baris_detail_miring['jumlah_item'] ?></td>
																	<td class="al_right currency item-amount" valign="middle" width="20%"><?php echo $baris_detail_miring['amount_miring'] ?></td>
																</tr>
															<?php
															$urut_miring++;
															$grand_total = $grand_total+$baris_detail_miring['amount_miring'];
														}
														?>
														</table>
													</td>
												</tr>
											</table>
										</td>
									</tr>
									<?php
								}
								$urut_menu++;
							}
						}
					}
				}
				?>
				<tr class="grand-total">
					<td class="al_right" colspan="4">Total Amount</td>
					<td class="al_right currency"><?php echo $grand_total ?></td>
				</tr>
				<?php
			}
			?>
			<script type="text/javascript">
				$(document).ready(function() {

					$('.currency').autoNumeric('init', {
						aSign: 'IDR ',
						aPad: false
					});

					$('.detail-item-placer').each(function(index, el) {
						var sub_amount = 0;
						var sub_jumlah = 0;
						var that_detail = $(this);
						that_detail.find('tr').each(function(index, el) {
							var that_row = $(this);
							sub_amount += parseInt(that_row.find('.item-amount').autoNumeric('get'));
							sub_jumlah += parseInt(that_row.find('.item-sum').html());
						});
						// that_detail.closest(".main-item-placer").find('.sub-amount').autoNumeric('set', sub_amount);
						// that_detail.closest(".main-item-placer").find('.sub-sum').html(sub_jumlah);
					});
				});
			</script>
			<?php
			break;
		
		case 'monthviewrestaurant':
			$grand_total = 0;
			$sql_year = "SELECT id AS id_resto_real, tanggal, MONTH(tanggal) AS month_select, DATE_FORMAT(tanggal, '%d') AS date_select, 
			DATE_FORMAT(tanggal, '%M %d, %Y') AS resto_date FROM resto 
			WHERE CONCAT(YEAR(tanggal),'-',MONTH(tanggal)) = '".$date."' 
			AND id IN (SELECT idresto FROM nota WHERE is_paid = 1 AND is_service_charge = 0) 
			AND id IN (SELECT idresto FROM resto_menu)
			GROUP BY DATE_FORMAT(tanggal, '%d') ASC";
			$query_year = mysql_query($sql_year);
			$count_year = mysql_num_rows($query_year);
			if ($count_year > 0) {
				$urut_menu = 1;
				$query_year = mysql_query($sql_year);
				while ($baris_year = mysql_fetch_array($query_year)) {
					$sql_month = "SELECT id AS id_resto_real, tanggal, MONTH(tanggal) AS month_select, DATE_FORMAT(tanggal, '%d') AS date_select, 
					DATE_FORMAT(tanggal, '%M %d, %Y') AS resto_date FROM resto 
					WHERE CONCAT(YEAR(tanggal),'-',MONTH(tanggal)) = '".$date."' AND DATE_FORMAT(tanggal, '%d') = '".$baris_year['date_select']."'
					AND id IN (SELECT idresto FROM nota WHERE is_paid = 1 AND is_service_charge = 0) 
					AND id IN (SELECT idresto FROM resto_menu)
					ORDER BY DATE_FORMAT(tanggal, '%d') ASC";
					$query_month = mysql_query($sql_month);
					$count_month = mysql_num_rows($query_month);
					if ($count_month > 0) {
						?>
						<tr class="list-date">
							<td colspan="5"><?php echo $baris_year['resto_date'] ?></td>
						</tr>
						<?php
						$sql_list_item = "SELECT * FROM resto_menu a
						WHERE a.idresto IN (SELECT id FROM resto WHERE CONCAT(YEAR(tanggal),'-',MONTH(tanggal)) = '".$date."' 
						AND DATE_FORMAT(tanggal, '%d') = '".$baris_year['date_select']."') GROUP BY a.idmenu ASC";
						$query_list_item = mysql_query($sql_list_item);
						$count_list_item = mysql_num_rows($query_list_item);
						if ($count_list_item > 0) {
							$query_cek_item = mysql_query($sql_list_item);
							while ($baris_cek_item = mysql_fetch_array($query_list_item)) {
								$sql_cek_per_item = "SELECT b.id AS id_menu, b.nama AS nama_menu, 
								a.harga_ori AS harga_menu, a.harga AS harga_jual FROM resto_menu a
								LEFT JOIN menu b ON b.id = a.idmenu
								WHERE a.idmenu = ".$baris_cek_item['idmenu']." AND a.idresto IN 
								(SELECT id FROM resto WHERE CONCAT(YEAR(tanggal),'-',MONTH(tanggal)) = '".$date."' 
								AND DATE_FORMAT(tanggal, '%d') = '".$baris_year['date_select']."')";
								$query_cek_per_item = mysql_query($sql_cek_per_item);
								$hasil_cek_item = 0;
								while ($baris_cek_per_item = mysql_fetch_array($query_cek_per_item)) {
									if ($baris_cek_per_item['harga_menu'] != $baris_cek_per_item['harga_jual']) {
										$hasil_cek_item++;
									}
								}
								if ($hasil_cek_item < 1) {
									$sql_detail_item = "SELECT b.id AS id_menu, b.nama AS nama_menu, a.harga_ori AS harga_menu, 
									SUM(a.jumlah) AS sum_item, (SUM(a.jumlah)*a.harga_ori) AS amount_item FROM resto_menu a
									LEFT JOIN menu b ON b.id = a.idmenu
									WHERE a.idmenu = ".$baris_cek_item['idmenu']." AND a.idresto IN 
									(SELECT id FROM resto WHERE CONCAT(YEAR(tanggal),'-',MONTH(tanggal)) = '".$date."' 
									AND DATE_FORMAT(tanggal, '%d') = '".$baris_year['date_select']."')";
									$query_detail_item = mysql_query($sql_detail_item);
									$baris_detail_item = mysql_fetch_array($query_detail_item);
									?>
									<tr class="list-item-main">
										<td class="al_center" valign="middle"><?php echo $urut_menu ?></td>
										<td class="al_left" valign="middle"><?php echo $baris_detail_item['nama_menu'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $baris_detail_item['harga_menu'] ?></td>
										<td class="al_center" valign="middle"><?php echo $baris_detail_item['sum_item'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $baris_detail_item['amount_item'] ?></td>
									</tr>
									<?php
									$grand_total = $grand_total+$baris_detail_item['amount_item'];
								}else{
									$sql_harga_miring = "SELECT b.id AS id_menu, b.nama AS nama_menu, 
									a.harga_ori AS harga_menu, a.harga AS harga_jual FROM resto_menu a
									LEFT JOIN menu b ON b.id = a.idmenu
									WHERE a.idmenu = ".$baris_cek_item['idmenu']." AND a.idresto IN 
									(SELECT id FROM resto WHERE CONCAT(YEAR(tanggal),'-',MONTH(tanggal)) = '".$date."' 
									AND DATE_FORMAT(tanggal, '%d') = '".$baris_year['date_select']."')
									GROUP BY a.harga DESC";
									$query_nama_miring = mysql_query($sql_harga_miring);
									$baris_nama_miring = mysql_fetch_array($query_nama_miring);
									?>
									<tr class="list-item-main">
										<td colspan="5" style="padding:0px;">
											<table width="100%" class="main-item-placer">
												<tr class="baris-main">
													<td class="al_center" valign="middle" width="10%"><?php echo $urut_menu ?></td>
													<td class="al_left" valign="middle" width="50%"><?php echo $baris_nama_miring['nama_menu'] ?></td>
													<td class="al_left" valign="middle" width="7%"><b><i>(original price)</i></b></td>
													<td class="al_left currency" valign="middle" width="13%" style="border-left:none"><?php echo $baris_nama_miring['harga_menu'] ?></td>
													<td class="al_right" valign="middle" width="20%" style="border-left:none"></td>
												</tr>

												<tr class="list-item-detail">
													<td colspan="5" style="padding:0px;">
														<table width="100%" class="detail-item-placer">
														<?php
														$query_harga_miring = mysql_query($sql_harga_miring);
														$urut_miring = 1;
														while ($baris_harga_miring = mysql_fetch_array($query_harga_miring)) {
															$sql_detail_miring = "SELECT SUM(a.jumlah) AS jumlah_item, a.harga, SUM(a.jumlah)*a.harga AS amount_miring, b.nama AS nama_menu 
															FROM resto_menu a LEFT JOIN menu b ON b.id = a.idmenu
															WHERE a.idmenu = ".$baris_cek_item['idmenu']." AND a.harga = '".$baris_harga_miring['harga_jual']."' AND a.idresto IN 
															(SELECT id FROM resto WHERE CONCAT(YEAR(tanggal),'-',MONTH(tanggal)) = '".$date."' 
															AND DATE_FORMAT(tanggal, '%d') = '".$baris_year['date_select']."')";
															$query_detail_miring = mysql_query($sql_detail_miring);
															$baris_detail_miring = mysql_fetch_array($query_detail_miring);
															?>
																<tr>
																	<td class="al_center" valign="middle" width="10%"></td>
																	<td class="al_left" valign="middle" width="50%"><i class="fa fa-angle-right"></i> <?php echo $baris_detail_miring['nama_menu']." (".$urut_miring.")" ?></td>
																	<td class="al_right currency" valign="middle" width="20%"><?php echo $baris_detail_miring['harga'] ?></td>
																	<td class="al_center item-sum" valign="middle" width="10%"><?php echo $baris_detail_miring['jumlah_item'] ?></td>
																	<td class="al_right currency item-amount" valign="middle" width="20%"><?php echo $baris_detail_miring['amount_miring'] ?></td>
																</tr>
															<?php
															$urut_miring++;
															$grand_total = $grand_total+$baris_detail_miring['amount_miring'];
														}
														?>
														</table>
													</td>
												</tr>
											</table>
										</td>
									</tr>
									<?php
								}
								$urut_menu++;
							}
						}
					}
				}
				?>
				<tr class="grand-total">
					<td class="al_right" colspan="4">Total Amount</td>
					<td class="al_right currency"><?php echo $grand_total ?></td>
				</tr>
				<?php
			}
			?>
			<script type="text/javascript">
				$(document).ready(function() {

					$('.currency').autoNumeric('init', {
						aSign: 'IDR ',
						aPad: false
					});

					$('.detail-item-placer').each(function(index, el) {
						var sub_amount = 0;
						var sub_jumlah = 0;
						var that_detail = $(this);
						that_detail.find('tr').each(function(index, el) {
							var that_row = $(this);
							sub_amount += parseInt(that_row.find('.item-amount').autoNumeric('get'));
							sub_jumlah += parseInt(that_row.find('.item-sum').html());
						});
						// that_detail.closest(".main-item-placer").find('.sub-amount').autoNumeric('set', sub_amount);
						// that_detail.closest(".main-item-placer").find('.sub-sum').html(sub_jumlah);
					});
				});
			</script>
			<?php
			break;

		case 'monthviewagent':
			$grand_total = 0;

			$sql_list_date = "SELECT DATE_FORMAT(a.checkout, '%Y-%c-%d') AS 'date_checkout', 
			DATE_FORMAT(a.checkout, '%M %d, %Y') AS 'date_tampil'
			FROM front_office a 
			WHERE a.is_paid = 1 AND CONCAT(YEAR(a.checkout),'-',MONTH(a.checkout)) = '".$date."' AND a.idagent != ''
			GROUP BY DATE_FORMAT(a.checkout, '%Y-%c-%d') ASC";
			$query_list_date = mysql_query($sql_list_date);
			$count_list_date = mysql_num_rows($query_list_date);
			if ($count_list_date > 0) {
				$query_list_date = mysql_query($sql_list_date);
				$i = 1;
				while ($baris_list_date = mysql_fetch_array($query_list_date)) {
					$sql_report = "SELECT * FROM front_office a 
					WHERE a.is_paid = 1 AND a.idagent != '' AND 
					DATE_FORMAT(a.checkout, '%Y-%c-%d') = '".$baris_list_date['date_checkout']."' 
					GROUP BY a.idagent ASC";
					$query_report = mysql_query($sql_report);
					if (mysql_num_rows($query_report) > 0) {
						?>
						<tr class="list-date">
							<td colspan="7"><?php echo $baris_list_date['date_tampil'] ?></td>
						</tr>
						<?php
						while ($baris_report = mysql_fetch_array($query_report)) {
							$sub_grand_total = 0;
							$sql_agent_service = "SELECT a.*, (DATE(a.checkout)-DATE(a.checkin)) as 'lama' FROM front_office a 
							WHERE a.is_paid = 1 AND DATE(a.checkout) = '".$baris_list_date['date_checkout']."' AND a.idagent = '".$baris_report['idagent']."'";
							$query_agent_service = mysql_query($sql_agent_service);
							$count_agent_service = mysql_num_rows($query_agent_service);

							$sql_agent = "SELECT * FROM agent WHERE id = '".$baris_report['idagent']."'";
							$query_agent = mysql_query($sql_agent);
							$result_agent = mysql_fetch_array($query_agent);
							?>
							<tr>
								<td class="al_center" valign="top" rowspan="<?php echo ($count_agent_service+2) ?>"><?php echo $i ?></td>
								<td class="al_left" valign="top" rowspan="<?php echo ($count_agent_service+2) ?>"><?php echo $result_agent['nama'] ?></td>
							</tr>
							<?php
								$query_agent_service = mysql_query($sql_agent_service);
								while ($baris_agent_service = mysql_fetch_array($query_agent_service)) {

									$data_room = getRoom($baris_agent_service['idroom']);
									$price_kamar = $baris_agent_service['real_paid'] * $baris_agent_service['lama'];
									$price_agent_service = (($price_kamar) * $result_agent['komisi'] / 100);

									$sql_status_paid = "SELECT is_paid, comission FROM agent_paid WHERE id_fo = '".$baris_agent_service['id']."' AND id_agent = '".$baris_report['idagent']."'";
									$query_status_paid = mysql_query($sql_status_paid);
									$status_paid = mysql_fetch_array($query_status_paid);

									if ($status_paid['is_paid'] == 1) {
										$paid_for_agent = "Paid";
									}else{
										$paid_for_agent = "Unpaid";
									}

									?>
									<tr>
										<td class="al_left" valign="middle"><?php echo $baris_agent_service['nama'] ?></td>
										<td class="al_center" valign="middle"><?php echo $data_room['nama'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $price_kamar ?></td>
										<td class="al_center" valign="middle"><?php echo $paid_for_agent ?></td>
										<td class="al_right currency" valign="middle"><?php echo $status_paid['comission'] ?></td>
									</tr>
									<?php
									$sub_grand_total = $sub_grand_total+$status_paid['comission'];
								}
							?>
							<tr class="sub-grand-total">
								<td class="al_right" colspan="4">Amount</td>
								<td class="al_right currency"><?php echo $sub_grand_total ?></td>
							</tr>
							<?php
							$grand_total = $grand_total+$sub_grand_total;
							$i++;
						}
					}
				}

				?>
				<tr class="grand-total">
					<td class="al_right" colspan="6">Total Amount</td>
					<td class="al_right currency"><?php echo $grand_total ?></td>
				</tr>
				<?php
			}else{
				?>
				<tr class="alert alert-error">
					<td colspan="7">No result has shown.</td>
				</tr>
				<?php
			}
			break;

		case 'yearviewagent':
			$grand_total = 0;

			$sql_list_date = "SELECT DATE_FORMAT(a.checkout, '%Y-%c-%d') AS 'date_checkout', 
			DATE_FORMAT(a.checkout, '%M %d, %Y') AS 'date_tampil'
			FROM front_office a 
			WHERE a.is_paid = 1 AND YEAR(a.checkout) = '".$date."' AND a.idagent != ''
			GROUP BY DATE_FORMAT(a.checkout, '%Y-%c-%d') ASC";
			$query_list_date = mysql_query($sql_list_date);
			$count_list_date = mysql_num_rows($query_list_date);
			if ($count_list_date > 0) {
				$query_list_date = mysql_query($sql_list_date);
				$i = 1;
				while ($baris_list_date = mysql_fetch_array($query_list_date)) {
					$sql_report = "SELECT * FROM front_office a 
					WHERE a.is_paid = 1 AND a.idagent != '' AND 
					DATE_FORMAT(a.checkout, '%Y-%c-%d') = '".$baris_list_date['date_checkout']."' 
					GROUP BY a.idagent ASC";
					$query_report = mysql_query($sql_report);
					if (mysql_num_rows($query_report) > 0) {
						?>
						<tr class="list-date">
							<td colspan="7"><?php echo $baris_list_date['date_tampil'] ?></td>
						</tr>
						<?php
						while ($baris_report = mysql_fetch_array($query_report)) {
							$sub_grand_total = 0;
							$sql_agent_service = "SELECT a.*, (DATE(a.checkout)-DATE(a.checkin)) as 'lama' FROM front_office a 
							WHERE a.is_paid = 1 AND DATE(a.checkout) = '".$baris_list_date['date_checkout']."' AND a.idagent = '".$baris_report['idagent']."'";
							$query_agent_service = mysql_query($sql_agent_service);
							$count_agent_service = mysql_num_rows($query_agent_service);

							$sql_agent = "SELECT * FROM agent WHERE id = '".$baris_report['idagent']."'";
							$query_agent = mysql_query($sql_agent);
							$result_agent = mysql_fetch_array($query_agent);

							?>
							<tr>
								<td class="al_center" valign="top" rowspan="<?php echo ($count_agent_service+2) ?>"><?php echo $i ?></td>
								<td class="al_left" valign="top" rowspan="<?php echo ($count_agent_service+2) ?>"><?php echo $result_agent['nama'] ?></td>
							</tr>
							<?php
								$query_agent_service = mysql_query($sql_agent_service);
								while ($baris_agent_service = mysql_fetch_array($query_agent_service)) {

									$data_room = getRoom($baris_agent_service['idroom']);
									$price_kamar = $baris_agent_service['real_paid'] * $baris_agent_service['lama'];
									$price_agent_service = (($price_kamar) * $result_agent['komisi'] / 100);

									$sql_status_paid = "SELECT is_paid, comission FROM agent_paid WHERE id_fo = '".$baris_agent_service[id]."' AND id_agent = '".$baris_report['idagent']."'";
									$query_status_paid = mysql_query($sql_status_paid);
									$status_paid = mysql_fetch_array($query_status_paid);

									if ($status_paid['is_paid'] == 1) {
										$paid_for_agent = "Paid";
									}else{
										$paid_for_agent = "Unpaid";
									}
									
									?>
									<tr>
										<td class="al_left" valign="middle"><?php echo $baris_agent_service['nama'] ?></td>
										<td class="al_center" valign="middle"><?php echo $data_room['nama'] ?></td>
										<td class="al_right currency" valign="middle"><?php echo $price_kamar ?></td>
										<td class="al_center" valign="middle"><?php echo $paid_for_agent ?></td>
										<td class="al_right currency" valign="middle"><?php echo $status_paid['comission'] ?></td>
									</tr>
									<?php
									$sub_grand_total = $sub_grand_total+$status_paid['comission'];
								}
							?>
							<tr class="sub-grand-total">
								<td class="al_right" colspan="4">Amount</td>
								<td class="al_right currency"><?php echo $sub_grand_total ?></td>
							</tr>
							<?php
							$grand_total = $grand_total+$sub_grand_total;
							$i++;
						}
					}
				}

				?>
				<tr class="grand-total">
					<td class="al_right" colspan="6">Total Amount</td>
					<td class="al_right currency"><?php echo $grand_total ?></td>
				</tr>
				<?php
			}else{
				?>
				<tr class="alert alert-error">
					<td colspan="7">No result has shown.</td>
				</tr>
				<?php
			}
			break;

		default:
			# code...
			break;
	}
?>